package com.cwnu.onlinemall.controller;

import com.cwnu.onlinemall.mapper.MallCommodityMapper;
import com.cwnu.onlinemall.pojo.MallCommodity;
import com.cwnu.onlinemall.pojo.ServerResult;
import com.cwnu.onlinemall.service.CommodityService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@CrossOrigin
@RestController
public class CommodityController {
    @Autowired
    CommodityService commodityService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/commodity/selectAll")
    public ServerResult selectAll(){
        if (commodityService.SelectAll()!=null) {
            return new ServerResult(0, "查询成功", commodityService.SelectAll());
        }else {
            return new ServerResult(1, "查询失败", null);
        }
    }

    @RequestMapping("/commodity/selectByCategoryName")
    public ServerResult selectByCategoryName(String categoryName){
        if (commodityService.selectByCategoryName(categoryName).size()>=1) {
            return  new ServerResult(0,"查询成功",commodityService.selectByCategoryName(categoryName));
        }else {
            return new ServerResult(1, "查询失败，可能输入字符有误", null);
        }
    }

    @RequestMapping("/commodity/selectById")
    public ServerResult selectById(Integer id) throws JsonProcessingException {
        //1.先从redis中查
        String redisString = stringRedisTemplate.opsForValue().get(id.toString());
        MallCommodity commodity=null;
        if (ObjectUtils.isEmpty(redisString)){
            //查不到再去数据库中查
            commodity=commodityService.selectById(id);
            return new ServerResult(0,"从mysql查询成功",commodity);
        }else {
            //查到了,把字符串转成对象
            ObjectMapper objectMapper=new ObjectMapper();
            commodity=objectMapper.readValue(redisString,MallCommodity.class);
        }
        return new ServerResult(0,"从redis查询成功",commodity);
    }

    @RequestMapping("/commodity/insert")
    public ServerResult insert(MallCommodity mallCommodity){
        int row=commodityService.insert(mallCommodity);
        if (row>=1) {
            return  new ServerResult(0,"插入成功",row);
        }else {
            return new ServerResult(1, "插入失败，可能输入字符有误", null);
        }
    }

    @RequestMapping("/commodity/update")
    public  ServerResult update(MallCommodity mallCommodity){
        int row=commodityService.updateById(mallCommodity);
        if (row>=1) {
            return  new ServerResult(0,"更新成功",row);
        }else {
            return new ServerResult(1, "更新失败，可能输入字符有误", null);
        }
    }

    @RequestMapping("/commodity/deleteById")
    public ServerResult delete(Integer id){
        int row=commodityService.deleteById(id);
        if (row>=1) {
            return  new ServerResult(0,"删除成功",row);
        }else {
            return new ServerResult(1, "删除失败，可能输入字符有误", null);
        }
    }

    @RequestMapping("/commodity/selectCommodityWriteToRedis")
    public ServerResult selectCommodityWriteToRedis(Integer id) throws JsonProcessingException {
        //1.从mysql中查询商品
        MallCommodity commodity = commodityService.selectById(id);
        //2.把对象转成字符串才能放入redis
        ObjectMapper objectMapper = new ObjectMapper();
        String string = objectMapper.writeValueAsString(commodity);

        //3.把字符串保存在redis中
        stringRedisTemplate.opsForValue().set(id.toString(),string);
        //4.返回数据
        return new ServerResult(0,"ok",null);
    }
}
